// import { fileURLToPath, URL } from 'node:url'
import * as path from "path";
import { resolve } from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import vueSetupExtend from 'vite-plugin-vue-setup-extend'

// https://vitejs.dev/config/
export default defineConfig({
  base: './',
  resolve: {
    // 配置别名
    alias: {
      '@': path.resolve(__dirname, './src')
    }
  },
  plugins: [
    vue(),
    vueSetupExtend(),
    createSvgIconsPlugin({
      iconDirs: [resolve(__dirname, 'src/assets/icons')],
      symbolId: 'icon-[dir]-[name]'
    })
  ],
  server: {
    host: '0.0.0.0',
    port: 3004,
    https: false,
    proxy: {
      '/api': {
        // target: 'http://10.100.50.27:9999', // 测试环境
        // target: 'http://10.100.50.43:9999', // 生产环境
        target: 'http://10.100.50.50:9999', // 2.0版本测试环境
        changeOrigin: true,
        ws: true, // 开启websocket代理
        rewrite: path => path.replace(/^\/api/, '')
      },
      '/eyetj': {
        target: 'http://10.100.50.50:3002',
        changeOrigin: true,
        rewrite: path => path.replace(/^\/eyetj/, '')
      },
      '/push': {
        target: 'http://10.100.10.15:1662',
        // target: 'http://127.0.0.1:3001',
        changeOrigin: true,
        rewrite: path => path.replace(/^\/push/, '')
      },
    }
  },
  build: {
    rollupOptions: {
      output: {
        // 最小化拆分包
        manualChunks: id => {
          if (id.includes('node_modules')) {
            return id.toString().split('node_modules/')[1].split('/')[0].toString()
          }
        },
        // 用于从入口点创建的块的打包输出格式
        entryFileNames: 'js/[name].[hash].js',
        // 用于命名代码拆分时创建的共享块的输出命名
        chunkFileNames: 'js/[name].[hash].js',
        // 用于输出静态资源的命名，[ext]表示文件扩展名
        assetFileNames: '[ext]/[name].[hash].[ext]'
      }
    }
  }
})
